package com.zrx.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrx.constants.SystemConstants;
import com.zrx.domain.ResponseResult;
import com.zrx.domain.entity.Link;

import com.zrx.domain.vo.LinkVo;
import com.zrx.domain.vo.PageVo;
import com.zrx.mapper.LinkMapper;

import com.zrx.service.LinkService;

import com.zrx.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 友链(SysLink)表服务实现类
 *
 * @author makejava
 * @since 2022-09-16 15:13:58
 */
@Service("LinkService")
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {


    /**
     * 查询友链 审核通过的
     * @return
     */
    @Override
    public ResponseResult getAllLink() {
        //查询所有审核通过的
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Link::getStatus, SystemConstants.LINK_STATUS_NORMAL);
        List<Link> links = list(queryWrapper);
        //转换vo
        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(links, LinkVo.class);
        return ResponseResult.okResult(linkVos);
    }

    @Override
    public ResponseResult<PageVo> pageLinkList(Integer pageNum, Integer pageSize, Link link) {
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(link.getName()),Link::getName,link.getName());
        queryWrapper.eq(StringUtils.hasText(link.getStatus()),Link::getStatus,link.getStatus());

        Page<Link> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);

        return ResponseResult.okResult(new PageVo(page.getRecords(),page.getTotal()));
    }
}

